#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QRegExpValidator>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->submitButton->setDisabled(true);
    ui->namelineEdit->setPlaceholderText("请输入姓名");
    ui->phonelineEdit->setPlaceholderText("请输入手机号");

    //注册正则表达式验证器: 注册的正则表达式一定要是堆上的！因为往后的执行逻辑应该是根据其地址来找到其内容而进行验证
    QRegExp reg("^1\\d{10}$");
    QRegExpValidator* validator = new QRegExpValidator(reg);
    ui->phonelineEdit->setValidator(validator);

    //简单限制格式输入
    //ui->phonelineEdit->setInputMask("000-0000-0000");
    ui->phonelineEdit->setClearButtonEnabled(true);

    ui->passwdlineEdit->setPlaceholderText("请输入密码");
    //ui->passwdlineEdit->setEchoMode(QLineEdit::EchoMode::PasswordEchoOnEdit);
    ui->passwdlineEdit->setEchoMode(QLineEdit::EchoMode::Password);
    ui->passwdlineEdit_2->setEchoMode(QLineEdit::EchoMode::Password);
    ui->passwdlineEdit->setClearButtonEnabled(true);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_submitButton_clicked()
{
    QString res;
    res += ui->namelabel->text()  + ui->namelineEdit->text() + " ";
    res += ui->sexlabel->text();
    if(ui->maleButton->isChecked()) res += ui->maleButton->text();
    else    res += ui->femaleButton->text();
    res += " ";
    res += ui->phonelabel->text() + ui->phonelineEdit->text() + " ";
    res += ui->passwdLabel->text() + ui->passwdlineEdit->text() + " ";

    qDebug() << res;
}

//验证手机号是否符合要求
void Widget::on_phonelineEdit_textEdited(const QString &arg1)
{
    int pos = 0;
    //QString str = arg1;
    if(ui->phonelineEdit->validator()->validate(const_cast<QString&>(arg1), pos) == QValidator::Acceptable)
    {
        ui->submitButton->setDisabled(false);
    }
    else
    {
        ui->submitButton->setDisabled(true);
    }
}

//验证两次输入的密码是否相同，注意需要对两个输入框都进行判断
void Widget::on_passwdlineEdit_textEdited(const QString &arg1)
{
    if(arg1 != ui->passwdlineEdit_2->text())
    {
        ui->submitButton->setDisabled(true);
    }
    else if(arg1 != nullptr)    //只用判断其中一个即可
    {
        ui->submitButton->setDisabled(false);
    }
}
void Widget::on_passwdlineEdit_2_textEdited(const QString &arg1)
{
    if(arg1 != ui->passwdlineEdit->text())
    {
        ui->submitButton->setDisabled(true);
    }
    else if(arg1 != nullptr)    //只用判断其中一个即可
    {
        ui->submitButton->setDisabled(false);
    }
}


//勾选改变密码显示样式
void Widget::on_checkBox_stateChanged(int arg1)
{
    if(arg1 == Qt::CheckState::Checked)
    {
        ui->passwdlineEdit->setEchoMode(QLineEdit::EchoMode::Normal);
    }
    else
    {
        ui->passwdlineEdit->setEchoMode(QLineEdit::EchoMode::Password);
    }
}
void Widget::on_checkBox_2_stateChanged(int arg1)
{
    if(arg1 == Qt::CheckState::Checked)
    {
        ui->passwdlineEdit_2->setEchoMode(QLineEdit::EchoMode::Normal);
    }
    else
    {
        ui->passwdlineEdit_2->setEchoMode(QLineEdit::EchoMode::Password);
    }
}
